package org.springblade.task.job.report.core;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.coreBoard.entity.DailyMarketingConversionReport;
import org.springblade.modules.coreBoard.service.IDailyMarketingConversionReportService;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.param.BaseShowLogParam;
import org.springblade.task.base.param.BaseUserClickLogParam;
import org.springblade.task.base.service.*;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.job.report.operation.DailyActivityTypeReportJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Title: 营销转化漏斗
 * @version V1.0
 */
@Component
public class DailyMarketingConversionReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseShowLogService baseShowLogService; // 曝光
	@Autowired
	private IBaseUserClickLogService baseUserClickLogService; // 用户点击  // 用户点击-加购
	// 电子书订单
	@Autowired
	private IBaseNetChapterOrderService BaseNetChapterOrderService;
	@Autowired
	private IBaseEbookOrderService BaseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService BaseAudioChapterOrderService;

	@Autowired
	private IDailyMarketingConversionReportService dailyMarketingConversionReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("DailyMarketingConversionReportJob")
	public ReturnT<String> DailyMarketingConversionReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyMarketingConversionReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyMarketingConversionReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		String startTime = DateUtils.getDateStartTime(taskDate);
		String endTime = DateUtils.getDayEndTime(taskDate);
		BaseShowLogParam logParam = new BaseShowLogParam();
		logParam.setStartDate(startTime);
		logParam.setEndDate(endTime);
		Integer pvCount = baseShowLogService.queryDailyPVCount(logParam); // 曝光数

		// 点击数
		BaseUserClickLogParam clickLogParam = new BaseUserClickLogParam();
		clickLogParam.setStartDate(startTime);
		clickLogParam.setEndDate(endTime);
		int userClickCount = baseUserClickLogService.queryUserClickCount(clickLogParam);
		// 用户点击加购物车数
		int userClickShoppingCartCount = baseUserClickLogService.queryUserClickShoppingCartCount(clickLogParam);

		// 付费订单数
		BaseOrderBookParam orderParam = new BaseOrderBookParam();
		orderParam.setStartDate(startTime);
		orderParam.setEndDate(endTime);
		int netChapterOrderNum = BaseNetChapterOrderService.queryPayOrderCount(orderParam);
		int ebookOrderNum = BaseEbookOrderService.queryPayOrderCount(orderParam);
		int audioChapterOrderNum = BaseAudioChapterOrderService.queryPayOrderCount(orderParam);
		DailyMarketingConversionReport report = new DailyMarketingConversionReport();
		report.setDate(taskDate);
		report.setExposureNum(pvCount);
		report.setClickNum(userClickCount);
		report.setJoinShoppingNum(userClickShoppingCartCount);
		report.setPaidOrderNum(netChapterOrderNum + ebookOrderNum+ audioChapterOrderNum);

		dailyMarketingConversionReportService.save(report);
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
